package com.example.demo.service;

import com.example.demo.entity.Paper;
import com.example.demo.repository.PaperRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class PaperServiceImpl implements PaperService {

    @Autowired
    private PaperRepository paperRepository;

    @Override
    public List<Paper> findAllPapers() {
        return paperRepository.findAll();
    }

    @Override
    public Paper savePaper(Paper paper) {
        return paperRepository.save(paper);
    }

    @Override
    public void deletePaper(Long id) {
        if (!paperRepository.existsById(id)) {
            throw new RuntimeException("Paper not found with id: " + id);
        }
        paperRepository.deleteById(id);
    }

    @Override
    public Paper updatePaper(Long id, Paper paperDetails) {
        Optional<Paper> optionalPaper = paperRepository.findById(id);
        if (optionalPaper.isPresent()) {
            Paper paper = optionalPaper.get();
            paper.setTitle(paperDetails.getTitle());
            // 更新其他字段...
            return paperRepository.save(paper);
        } else {
            throw new RuntimeException("Paper not found with id: " + id);
        }
    }

    @Override
    public Paper createPaper(Paper paper) {
        return paperRepository.save(paper);
    }

    @Override
    public Paper findPaperById(Long id) {
        return paperRepository.findById(id).orElse(null);
    }
}